package chixing.day05;

public class User {
    private int userId;
    private Book[] userBorrowedBookArray ;
    private int size = 0;
    private String userName;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public User(int userId, String userName){
        this.userId = userId;
        this.userName = userName;
        userBorrowedBookArray = new Book[5];
    }
    //借书
    public boolean borrowBook(Book book){
        if(book == null)
            return false;

        if(book.isAvailable()){
            book.setAvailable(false);  // 标记为不可借阅
            userBorrowedBookArray[size++] = book;
            System.out.println("用户" + this.userName + "借书" +book.getBookIsbn() + " 成功》》》》》》》》》》》");
            return true;
        }
        return false;
    }

    public boolean returnBook(Book book){
        if(book ==null)
            return false;
        for(int i=0;i<size;i++){
            // 还的书， 是该用户曾经借阅的书
            if(book.getBookIsbn() == userBorrowedBookArray[i].getBookIsbn()) { // 判断是不是该用户借的书
                System.out.println("用户" + this.userName + "还书" +book.getBookIsbn() + " 成功");
                book.setAvailable(true);
                // Book数组的元素 依次往前挪
                System.arraycopy(userBorrowedBookArray,i+1,userBorrowedBookArray,i,size-i-1);
                userBorrowedBookArray[size-1] = null; // 最后一个置为 null
                size--;
                return true;
            }
        }
        return false;
    }
    public void listBorrowedBooks(){
        for(int i=0;i<size;i++){
            System.out.println(userBorrowedBookArray[i]);
        }
    }

}
